<?php
namespace App\Storage;

use EasySwoole\Component\Singleton;
use EasySwoole\Component\TableManager;
use Swoole\Table;

class OnlineUser2 {

    use Singleton;
    protected $table; //储存用户信息的Table

    /**
     * 初始化 在线用户table
     * OnlineUser constructor.
     */
    function __construct()
    {
        TableManager::getInstance()->add('onlineUsers2',[
            'fd' => ['type' => Table::TYPE_INT, 'size' => 8],
            'avatar' => ['type' => Table::TYPE_STRING, 'size' => 128],
            'username' => ['type' => Table::TYPE_STRING, 'size' => 128],
            'shop_id' => ['type' => Table::TYPE_INT, 'size' => 8],
            'is_shop' => ['type' => Table::TYPE_INT, 'size' => 4], // 1商户,2非商户
            'user_id' => ['type' => Table::TYPE_INT, 'size' => 8],
            'last_heartbeat' => ['type' => Table::TYPE_INT, 'size' => 4], //用户最后一次访问
        ], 102400);

        $this->table = TableManager::getInstance()->get('onlineUsers');
    }

    /**
     * 设置一条用户信息。key 是fd，value是用户信息
     * @param $fd
     * @param $username
     * @param $avatar
     * @param $shopId
     * @param $isShop
     * @return mixed
     */
    function set($fd, $username, $avatar, $shopId, $isShop, $userId) {
        return $this->table->set($userId, [
            'fd'                => $fd,
            'avatar'            => $avatar,
            'username'          => $username,
            'shop_id'           => $shopId,
            'is_shop'           => $isShop,
            'user_id'           => $userId,
            'last_heartbeat'    => time(),
        ]);
    }

    /**
     * 获取一条用户信息
     * @param $fd
     * @return array|mixed|null
     */
    function get($userId) {
        $info = $this->table->get($userId);
        return is_array($info) ? $info : null;
    }

    /**
     * 更新一条用户信息
     * @param $fd
     * @param $data
     */
    function update($userId, $data) {
        $info = $this->get($userId);
        if ($info) {
            $fd = $info['user_id'];
            $info = $data + $info;
            $this->table->set($fd,$info);
        }
    }

    /**
     * 删除一条用户信息
     * @param $fd
     */
    function delete($userId) {
        $info = $this->get($userId);
        if ($info) {
            $this->table->del($info['user_id']);
        }
    }

    /**
     * 心跳更新
     * @param $fd
     */
    function updateHeartbeat($fd) {
        $this->update($fd,[
            'last_heartbeat' => time()
        ]);
    }

    /**
     * 心跳检测
     * @param int $ttl
     */
    function heartbeatCheck($ttl = 60) {
        foreach ($this->table as $item) {
            $time = $item['time'];
            if (($time + $ttl) < $time ) {
                $this->delete($item['user_id']);
            }
        }
    }

    /**
     * 返回当前table
     * @return Table|null
     */
    public function table() {
        return $this->table;
    }

    /**
     * 返回当前用户总数
     * @return mixed
     */
    public function count() {
        return $this->table->count();
    }

}
